/**
 * Created with JetBrains WebStorm.
 * User: huwei
 * Date: 13-8-22
 * Time: 下午1:09
 * To change this template use File | Settings | File Templates.
 */
Ext.ux.WindowAlwaysOnTop = function() {
    this.init = function(win) {
        win.on('deactivate', function() {
            var i = 1;
            this.manager.each(function() {
                i++
            });
            this.setZIndex(this.manager.zseed + (i * 10));
        })
    }
}

function showHandlerWin(url, title, width, height) {
    if (typeof(width) == 'undefined') {
        width = "100%";
    }
    if (typeof(height) == 'undefined') {
        height = "100%";
    }
    win = null;
    if (!win) {
        win = new Ext.Window({
            title: title,
            header: {
                titlePosition: 2,
                titleAlign: 'center'
            },
            closable: true,
            closeAction: 'hide',
            x: 0,
            y: 0,
            width: width,
            height: height,
            html: '<iframe width="100%" height="100%" frameborder="0" src="' + url + '"></iframe>'
        });
    }
    win.show();
}

function showHandlerWinTop(url, title, width, height) {
    if (typeof(width) == 'undefined') {
        width = "100%";
    }
    if (typeof(height) == 'undefined') {
        height = "100%";
    }
    win = null;
    if (!win) {
        win = new top.Ext.Window({
            title: title,
            header: {
                titlePosition: 2,
                titleAlign: 'center'
            },
            closable: true,
            closeAction: 'hide',
            x: 0,
            y: 0,
            width: width,
            height: height,
            html: '<iframe width="100%" height="100%" frameborder="0" src="' + url + '"></iframe>',
            bbar:[{
                text:'关闭',
                icon : '/admin/img/close.png',
                handler:function(){
                    this.up('window').destroy();
                }
            }]
        });
    }
    win.show();
}

function doEdit(grid, _url, title, width, height,top) {
    var orows = grid.getSelectionModel().getSelection();
    if (orows.length == 0) {
        Ext.MessageBox.show({
            title: "消息提示",
            msg: "请选择一条记录",
            width: 200,
            icon: Ext.MessageBox.INFO
        });
        return;
    }
    var url = "";
    if (_url.indexOf("?") >= 0) {
        url = _url + "&_id=" + orows[0].data._id;
    } else {
        url = _url + "?_id=" + orows[0].data._id;
    }
    if(top){
        showHandlerWinTop(url, title, width, height);
    }else{
        showHandlerWin(url, title, width, height);
    }
    
}

function doEditTop(grid, _url, title, width, height) {
    doEdit(grid, _url, title, width, height,true);
}

function doAdd(url, title, width, height) {
    showHandlerWin(url, title, width, height);
}

function doAddTop(url, title, width, height) {
    showHandlerWinTop(url, title, width, height);
}



function doDel(grid, url, store) {
    var orows = grid.getSelectionModel().getSelection();
    if (orows.length == 0) {
        Ext.MessageBox.show({
            title: "消息提示",
            msg: "请选择一条记录",
            width: 200,
            icon: Ext.MessageBox.INFO
        });
        return;
    }
    Ext.MessageBox.confirm('消息提示', '确定删除对象？', function(btn) {
        if (btn == "yes") {
            Ext.Ajax.request({
                url: url,
                params: {
                    _id: orows[0].get('_id')
                },
                method: 'POST',
                success: function(response, options) {
                    Ext.MessageBox.alert('消息提示', '操作成功！');
                    store.loadPage(1);
                },
                failure: function(response, options) {
                    // Ext.MessageBox.alert('消息提示', '操作失败！');
                    Ext.MessageBox.alert('消息提示', Ext.JSON.decode(response.responseText).error);
                }
            });
        }
    })
}

function uploadFile(_form, filename, image_ctl) {
    var url = "/file/upload";
    if (!_form.getForm().findField("attach_file").getValue()) {
        Ext.Msg.alert('消息提示', '请浏览需要上传的文件!');
        return;
    }
    if (filename) {
        url += '?filename=' + filename;
    }
    _form.form.submit({
        url: url,
        waitMsg: '正在提交数据，稍后...',
        success: function(form, action) {
            _form.getForm().findField("image_file_id").setValue(action.result.file_id);
            if (image_ctl) {
                if (filename) {
                    image_ctl.setSrc('/_image/' + filename);
                } else {
                    image_ctl.setSrc('/_image/' + action.result.file_id);
                }

            }
            Ext.Msg.alert('消息提示', '上传成功，请按保存继续操作！');
        },
        failure: function(form, action) {
            if (action && action.response && action.response.responseText) {
                Ext.Msg.alert('消息提示', Ext.JSON.decode(action.response.responseText).error);
            } else {
                Ext.Msg.alert('消息提示', '上传失败！');
            }

        }
    });
}

function uploadFile2(_form, filename, image_ctl) {
    var url = "/file/upload2";
    if (!_form.getForm().findField("attach_file_2").getValue()) {
        Ext.Msg.alert('消息提示', '请浏览需要上传的文件!');
        return;
    }
    if (filename) {
        url += '?filename=' + filename;
    }
    _form.form.submit({
        url: url,
        waitMsg: '正在提交数据，稍后...',
        success: function(form, action) {
            _form.getForm().findField("attach_file_id").setValue(action.result.file_id);
            if (image_ctl) {
                if (filename) {
                    image_ctl.setSrc('/_image/' + filename);
                } else {
                    image_ctl.setSrc('/_image/' + action.result.file_id);
                }

            }
            Ext.Msg.alert('消息提示', '上传成功，请按保存继续操作！');
        },
        failure: function(form, action) {
            if (action && action.response && action.response.responseText) {
                Ext.Msg.alert('消息提示', Ext.JSON.decode(action.response.responseText).error);
            } else {
                Ext.Msg.alert('消息提示', '上传失败！');
            }

        }
    });
}

function uploadFileCb(_form, filename,fieldname,callback) {
    var url = "/file/upload";
    fieldname = fieldname || "attach_file"
    if (!_form.getForm().findField(fieldname).getValue()) {
        return callback("请浏览需要上传的文件(attach_file)");
        return;
    }
    if (filename) {
        url += '?filename=' + filename;
    }
    _form.form.submit({
        url: url,
        waitMsg: '正在提交数据，稍后...',
        success: function(form, action) {
            return callback(null, action.result);
        },
        failure: function(form, action) {
            if (action && action.response && action.response.responseText) {
                return callback(Ext.JSON.decode(action.response.responseText).error);
            } else {
                return callback('上传失败！');
            }
        }
    });
}

function resizeImage(srcfile, width, filename) {
    Ext.Ajax.request({
        url: '/img_handler/resize',
        params: {
            srcfile: srcfile,
            width: width,
            filename: filename
        },
        method: 'get',
        success: function(response, options) {
            Ext.MessageBox.alert('消息提示', '生成小图标成功！');
        },
        failure: function(response, options) {
            Ext.MessageBox.alert('消息提示', '生成小图标失败！');
        }
    });
}

function uploadFile2Server(_form, filefield_name, submitfield_name) {
    var url = "/file/common-upload?filefield_name="+filefield_name;
    if (!_form.getForm().findField(filefield_name).getValue()) {
        Ext.Msg.alert('消息提示', '请选择需要上传的文件!');
        return;
    }
    _form.form.submit({
        url: url,
        waitMsg: '正在提交数据，稍后...',
        success: function(form, action) {
            _form.getForm().findField(submitfield_name).setValue(action.result.file_id);
            Ext.Msg.alert('消息提示', '上传成功，请按保存继续操作！');
        },
        failure: function(form, action) {
            if (action && action.response && action.response.responseText) {
                Ext.Msg.alert('消息提示', Ext.JSON.decode(action.response.responseText).error);
            } else {
                Ext.Msg.alert('消息提示', '上传失败！');
            }

        }
    });
}

Date.prototype.format = function(fmt) {
 var o = {
     "M+": this.getMonth() + 1,
     //月份
     "d+": this.getDate(),
     //日
     "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12,
     //小时
     "H+": this.getHours(),
     //小时
     "m+": this.getMinutes(),
     //分
     "s+": this.getSeconds(),
     //秒
     "q+": Math.floor((this.getMonth() + 3) / 3),
     //季度
     "S": this.getMilliseconds() //毫秒
 };
 var week = {
     "0": "\u65e5",
     "1": "\u4e00",
     "2": "\u4e8c",
     "3": "\u4e09",
     "4": "\u56db",
     "5": "\u4e94",
     "6": "\u516d"
 };
 if (/(y+)/.test(fmt)) {
     fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
 }
 if (/(E+)/.test(fmt)) {
     fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f": "\u5468") : "") + week[this.getDay() + ""]);
 }
 for (var k in o) {
     if (new RegExp("(" + k + ")").test(fmt)) {
         fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
     }
 }
 return fmt;
}